/*
 * Copyright 2010 JooKar (info@jookar.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 * More information to be found at:
 * http://code.google.com/p/support/wiki/IssueTrackerAPIJava
 */
package nl.jookar.gcodecommithook;
import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import nl.jookar.gcodecommithook.handler.HandlerException;
import nl.jookar.gcodecommithook.handler.RequestHandler;

public class GCodeCommitHookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final Logger LOG = Logger.getLogger(GCodeCommitHookServlet.class.getName());
	/**
	 * {@inheritDoc}
	 */
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
		resp.setHeader("Location", "index.html");
	}

	/**
	 * {@inheritDoc}
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			RequestHandler.handleRequest(req);
			resp.setStatus(HttpServletResponse.SC_OK);
		} catch (HandlerException e) {
			LOG.warning("Error processing request. Returning response: " + e.getErrorCode());
			LOG.warning("Exception thrown: " + e.getClass().getName() + "; message: " + (e.getMessage()!=null?e.getMessage():"No message"));
			resp.setStatus(e.getErrorCode());
		}
		resp.flushBuffer();
	}
	
	
}
